package com.yonyou.lxp.account.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;

import com.yonyou.lxp.account.R;
import com.yonyou.lxp.account.entity.TypeInfo;

import org.xutils.common.util.LogUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by liuxiaopeng on 16/2/29.
 */
public class DataAdapter extends RecyclerView.Adapter<DataAdapter.MyViewHolder> {
    private List<TypeInfo> list;
    private Context context;
    private Map<Integer,Boolean> map=new HashMap<Integer ,Boolean>();

    public DataAdapter(List<TypeInfo> list, Context context){
        this.list=list;
        this.context=context;
    }
    @Override
    public DataAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_a, parent,
                false));
        return holder;
    }

    @Override
    public void onBindViewHolder(DataAdapter.MyViewHolder holder, final int position) {
        holder.checkBox.setText(list.get(position).getT_name());
        final Integer integer=new Integer(position);
        holder.checkBox.setTag(integer);
        if (integer==position) {
            if (map.containsKey(integer)) {
                holder.checkBox.setChecked(true);
            } else {
                holder.checkBox.setChecked(false);
            }
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    class MyViewHolder extends RecyclerView.ViewHolder {
        private CheckBox checkBox;
        public MyViewHolder(View view) {
            super(view);
            checkBox = (CheckBox) view.findViewById(R.id.checkBox);
            checkBox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                   Integer mFlags= (Integer) checkBox.getTag();
                    LogUtil.e(mFlags+"===========");


                    if (map.containsKey(mFlags)) {
                        map.remove(mFlags);
                    } else {
                        map.put(mFlags, Boolean.TRUE);
                    }
                }
            });
        }
    }

    public void setAll(boolean isAll){
        if (isAll){
            for (int i = 0; i < list.size(); i++) {
                map.put(i, Boolean.TRUE);
            }
        }else {
            map.clear();
        }
        notifyDataSetChanged();
    }

    public Map<Integer ,Boolean> getMap(){
        return map;
    }

}
